<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="food">
        <el-input
          v-model="queryParams.food"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="status">
      <el-select v-model="queryParams.sid" placeholder="请选择供应商" clearable>
        <el-option
            v-for="item in supplierGroup"
            :key="item.sid"
            :label="item.supplierName"
            :value="item.sid"
        />
      </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in shop_food_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>

        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>

      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['shop:food:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shop:food:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shop:food:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['shop:food:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="foodList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品名称" align="center" prop="food" />
      <el-table-column label="供应商" align="center" prop="supplierName" />
      <el-table-column label="商品产地" align="center" prop="origin" />
      <el-table-column label="进货单价" align="center" prop="price" />
      <el-table-column label="商品库存" align="center" prop="stock">
        <template #default="scope">
          <span :class="{'text-danger': scope.row.stock < 50, 'text-success': scope.row.stock > 100}">{{ scope.row.stock }}</span>
        </template>
      </el-table-column>

      <el-table-column label="商品规格" align="center" prop="specification" />
      <el-table-column label="包装单位" align="center" prop="unit" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="shop_food_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" :min-width="150" >
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['shop:food:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['shop:food:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改食品表对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="foodRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名称" prop="food">
          <el-input v-model="form.food" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="供应商" prop="status">
          <el-select v-model="form.sid" placeholder="请选择供应商" clearable>
            <el-option
                v-for="item in supplierGroup"
                :key="item.sid"
                :label="item.supplierName"
                :value="item.sid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="商品产地" prop="origin">
          <el-input v-model="form.origin" placeholder="请输入商品产地" />
        </el-form-item>
        <el-form-item label="销售价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入销售价格" />
        </el-form-item>
        <el-form-item label="商品库存" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入商品库存" />
        </el-form-item>
        <el-form-item label="商品规格" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入商品规格" />
        </el-form-item>
        <el-form-item label="包装单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入包装单位" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in shop_food_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Food">
import {listFood, getFood, delFood, addFood, updateFood} from "@/api/shop/food";
import {listSupplier} from "@/api/shop/supplier";
import {onMounted} from "vue";
import {listDate1, listDate3} from "@/api/shop/date";
import {listSell2} from "@/api/shop/sell";

const { proxy } = getCurrentInstance();
const { shop_food_status } = proxy.useDict('shop_food_status');
const { shop_food_supplier_group } = proxy.useDict('shop_food_supplier_group');

const foodList = ref([]);
const supplierGroup =  ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    food: null,
    sid:null
  },

  rules: {
    food: [
      { required: true, message: "商品名称不能为空", trigger: "blur" }
    ],
    sid: [
      { required: true, message: "供应商ID不能为空", trigger: "blur" }
    ],
    origin: [
      { required: true, message: "商品产地不能为空", trigger: "blur" }
    ],
    price: [
      { required: true, message: "销售价格不能为空", trigger: "blur" }
    ],
    stock: [
      { required: true, message: "商品库存不能为空", trigger: "blur" }
    ],
    specification: [
      { required: true, message: "商品规格不能为空", trigger: "blur" }
    ],
    unit: [
      { required: true, message: "包装单位不能为空", trigger: "blur" }
    ],

  }
});

const { queryParams, form, rules} = toRefs(data);


/** 查询食品表列表 */
function getList() {
  getSupplier()
  loading.value = true;
  listFood(queryParams.value).then(response => {
    foodList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}



function getSupplier(){
  listSupplier().then(response =>{
    supplierGroup.value= response.rows;
    console.log(supplierGroup.value)
  })

}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    fid: null,
    food: null,
    sid: null,
    origin: null,
    price: null,
    stock: null,
    specification: null,
    unit: null,
    status: null
  };
  proxy.resetForm("foodRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;

  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.sid=null
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.fid);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加食品表";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _fid = row.fid || ids.value
  getFood(_fid).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改食品表";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["foodRef"].validate(valid => {
    if (valid) {
      if (form.value.fid != null) {
        updateFood(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFood(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _fids = row.fid || ids.value;
  proxy.$modal.confirm('是否确认删除食品表编号为"' + _fids + '"的数据项？').then(function() {
    return delFood(_fids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('shop/food/export', {
    ...queryParams.value
  }, `food_${new Date().getTime()}.xlsx`)
}

getList();

</script>
